package com.yourname.xhs.controller;

import com.yourname.xhs.bean.Announce;
import com.yourname.xhs.bean.Category;
import com.yourname.xhs.bean.Note;
import com.yourname.xhs.entity.User;
import com.yourname.xhs.service.AnnounceServiceImpl;
import com.yourname.xhs.service.NoteServiceImpl;
import com.yourname.xhs.service.UserServiceImpl;
import com.yourname.xhs.util.DES;
import com.yourname.xhs.util.ValidateUtiil;
import com.yourname.xhs.view.AdminHomePage;
import com.yourname.xhs.view.MenuPage;
import com.yourname.xhs.view.UserHomePage;

import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.List;
import java.util.Scanner;

import static com.yourname.xhs.service.UserServiceImpl.tuser;
import static com.yourname.xhs.util.DES.desEncript;

public class UserControllerImpl implements UserController {
    UserServiceImpl usi = new UserServiceImpl();//service层
    NoteServiceImpl nni = new NoteServiceImpl();//service层
    AnnounceServiceImpl asi = new AnnounceServiceImpl();//service层


    AdminHomePage ahp = new AdminHomePage();//view
    UserHomePage uhp = new UserHomePage();//view
    MenuPage mp = new MenuPage();//view
    Scanner sc = new Scanner(System.in);



    public static String originKey = "12345678";//密钥
    public static String cleartext;//明文（密码）

    Note note = new Note();
    User user = new User();



   ValidateUtiil   vlu=new ValidateUtiil();

    //主页面的实现
    public void Menu() throws Exception {

      UserControllerImpl usl=new UserControllerImpl();
      String originKey = "12345678";
        while (true) {
            try {
                int flag = sc.nextInt();
                sc.nextLine();
                switch (flag) {
                    case 1:
                        boolean i = login(user);
                        if (i) {
                            boolean j = usi.isAdmin(user);
                            if (j) {
                                uhp.main();
                            } else {
                                ahp.main();
                            }
                        } else {
                            mp.main();
                        }
                        break;
                    case 2:
                        usl.register(user);//注册
                        break;
                    case 0:
                        System.exit(0);//退出
                        break;
                    default:
                        System.out.println("输入错误");
                        break;

                }
            } catch (InputMismatchException e) {
                System.out.println("-----------------------");
                System.out.println("1、登录");
                System.out.println("2、注册");
                System.out.println("0、退出");
                System.out.println("-----------------------");
                System.out.println("错误！请输入一个有效的数字选项！");
                sc.nextLine(); // 清除非法输入，准备接收下一次输入
            }
        }
    }


    //注册页面实现
    public void register(User user) throws Exception {
        System.out.println("请输入用户名：");
        user.setUserName(sc.next());
        System.out.println("请输入密码：");
        String password1 = sc.next();
        System.out.println("请再次输入密码：");
        String password2 = sc.next();
        if (password1.equals(password2)) {
            user.setPassword(password1);
            boolean j = usi.register(user);
            if (j) {
                System.out.println("注册成功！");
            } else {
                System.out.println("注册失败！");
            }
        } else {
            System.out.println("密码不一致，注册失败！");
        }
        user.setIsadmin("admin");
        mp.main();
    }

    //登录页面实现
    public boolean login(User user) throws Exception {
        System.out.println("请输入用户名：");
        user.setUserName(sc.next());
        System.out.println("请输入密码：");
        String password = sc.next();
        User newuser = new User();
        newuser = usi.login(user.getUserName(), password);
        user = newuser;
        if (user != null) {
            return true;
        } else {
            System.out.println("登录失败！");
            return false;
        }

    }


    //用户主页面的实现
    public void User() throws Exception {
        List<Announce> announceList = new ArrayList<>();
        List<Note> notesList = new ArrayList<>();
        while (true) {
            try {
                int flag = sc.nextInt();
                sc.nextLine();
                int control;
                switch (flag) {
                    case 0:   //个人信息
                        usi.getUserInfo(tuser);
                        System.out.println("----------------------");
                        System.out.println("1、修改密码");
                        System.out.println("2、修改个人信息");
                        System.out.println("3、返回上一级");
                        System.out.println("----------------------");
                        control = sc.nextInt();
                        if (control == 1) {

                            System.out.println("请输入新的密码：");
                            cleartext = sc.next();
                            //加密
                            boolean i = usi.modifyPassword(tuser, cleartext);
                            if (i) {
                                System.out.println("修改成功！请重新登录。");
                                mp.main();
                            }
                        } else if (control == 2) {
                            System.out.println("请输入新的个人信息：");
                            String Username = sc.next();
                            int line = usi.modifyInfo(tuser, Username);
                            System.out.println(tuser);
                            System.out.println(tuser.getUserName());
                            System.out.println(tuser.getUserId());
                            System.out.println(tuser.getPassword());
                            if (line > 0) {
                                System.out.println("修改成功！");
                                return;
                            }
                        } else if (control == 3) {
                            uhp.main();
                        } else {
                            System.out.println("输入错误！");
                            return;
                        }
                        break;
                    case 1: //查看通告
                        announceList = asi.selectAnnounce();
                        if(vlu.validate(announceList)) {
                            System.out.println("-----------");
                            for (Announce announce : announceList) {
                                System.out.println(announce);
                            }
                            System.out.println("-----------");
                        }
                        else {
                            System.out.println("暂无通告");
                        }
                        return;

                    case 2: //退出系统
                        System.out.println("是否确定退出登录？");
                        System.out.println("1、确定");
                        System.out.println("2、取消");
                        int c = sc.nextInt();
                        if (c == 1) {
                            mp.main();
                        } else {
                            uhp.main();
                        }
                        break;
                    case 3: //发布笔记
                        System.out.println("请输入标题：");
                        String title = sc.next();
                        System.out.println("请输入正文：");
                        String content = sc.next();
                        if (nni.CreateNote(title, content,tuser)) {
                            System.out.println("发布成功！");
                        } else {
                            System.out.println("发布失败！");
                        }
                        return;
                    case 4:  //删除笔记
                        int line;
                        notesList = nni.selectPersonalNote(tuser);
                        System.out.println("----------------");
                        for (Note note : notesList) {
                            System.out.println(note);
                        }
                        System.out.println("----------------");
                        System.out.println("请输入要删除笔记的id:");
                        Long id = sc.nextLong();
                        line = nni.deletePersonalNoteById(id,tuser);
                        if (line > 0) {
                            System.out.println("删除成功！");
                        } else {
                            System.out.println("删除失败！");
                        }
                        return;



    case 5://修改笔记
    notesList=nni.selectPersonalNote(tuser);
    System.out.println("------------------");
    for (Note note : notesList) {
        System.out.println(note);
    }
    System.out.println("------------------");
    System.out.println("请输入要修改的笔记的id：");
    id = sc.nextLong();
    System.out.println("------------------");
    System.out.println("请输入要修改的内容：");
    System.out.println("1、标题");
    System.out.println("2、内容");
    System.out.println("3、分类");
    System.out.println("------------------");
    control = sc.nextInt();
    sc.nextLine();
    switch (control) {
        case 1:
            System.out.println("请输入新标题：");
            String newTitle = sc.next();
            line = nni.updateTitle(id,tuser, newTitle);
            if (line > 0) {
                System.out.println("修改成功！");
            } else {
                System.out.println("修改失败！");
            }

            return;
        case 2:
            System.out.println("请输入新内容：");
            String newContent = sc.next();
            line = nni.updateTitle(id, tuser, newContent);
            if (line > 0) {
                System.out.println("修改成功！");
            } else {
                System.out.println("修改失败！");
            }
            return;
        case 3:
            System.out.println("请选择新的类别：");
            System.out.println("1. 知识");
            System.out.println("2. 美食");
            System.out.println("3. 旅行");
            System.out.println("4. 运动");
            System.out.println("5. 生活");
            System.out.println("6. 科技");
            control = sc.nextInt();
            String newCategoryName = Category.getCategoryByIndex(control).getName();
            if (newCategoryName != null) {
                line = nni.updateCategory(id, tuser, newCategoryName);
                if (line > 0) {
                    System.out.println("修改成功！");
                } else {
                    System.out.println("修改失败！");
                }
            }
    }
    return;





                    case 6:  //查看个人笔记
                        notesList = nni.selectPersonalNote(tuser);
                        System.out.println("----------------");
                        for (Note note : notesList) {
                            System.out.println(note);
                        }
                        System.out.println("----------------");
                        return;
                    case 7:  //查看分类笔记
                        notesList = nni.selectTypeNote();
                        System.out.println("-----------------");
                        for (Note note : notesList) {
                            System.out.println(note);
                        }
                        System.out.println("-----------------");
                        return;
                    case 8: //搜索笔记
                        System.out.println("请输入关键词：");
                        String keyword = sc.next();
                        notesList = nni.searchNotesByTitle(keyword);
                        System.out.println("------------------");
                        for (Note note : notesList) {
                            System.out.println(note);
                        }
                        System.out.println("------------------");
                        return;
                    default:
                        break;
                }
            } catch (InputMismatchException e) {
                System.out.println("-----------------------");
                System.out.println("0、个人信息\t    1、查看通告\t    2、退出登录");
                System.out.println("3、创建笔记\t    4、删除笔记\t    5、修改笔记");
                System.out.println("6、查看全部笔记\t7、查看分类笔记\t8、搜索笔记(标题）\t");
                System.out.println("-----------------------");
                System.out.println("错误！请输入一个有效的数字选项！");
                sc.nextLine(); // 清除非法输入，等待下一次有效输入
            }

        }
    }

    //管理员界面的实现
    public void Admin() throws Exception {
        Scanner sc = new Scanner(System.in);
        List<User> usersList = new ArrayList<>();
        List<Note> notesList = new ArrayList<>();
        List<Announce> announceList = new ArrayList<>();
        StringBuilder sb=new StringBuilder();
        while (true) {
            try {
                int flag = sc.nextInt();
                sc.nextLine();
                Long id;
                switch (flag) {
                    case 0:    //个人信息
                      usi.getUserInfo(tuser);
                        System.out.println("----------------------");
                        System.out.println("1、修改密码");
                        System.out.println("2、修改个人信息");
                        System.out.println("3、返回上一级");
                        System.out.println("----------------------");
                        int c = sc.nextInt();
                        if (c == 1) {
                            System.out.println("请输入新的密码：");
                            String cleartext = sc.next();
                            //加密
                            boolean i = usi.modifyPassword(user, cleartext);
                            if (i) {
                                System.out.println("修改成功！");
                                return;
                            }
                        } else if (c == 2) {
                            System.out.println("请输入新的个人信息：");
                            String Username = sc.next();
                            int line = usi.modifyInfo(user, Username);
                            if (line>0) {
                                System.out.println("修改成功！");
                                return;
                            }
                        } else if (c == 3) {
                           return;
                        } else {
                            System.out.println("输入错误！请重新输入：");
                            return;
                        }
                        break;
                    case 1:
                        System.out.println("请输入通告内容：");
                        String content = sc.next();
                        if (asi.createAnnounce(content)) {
                            System.out.println("发布成功！");
                        } else {
                            System.out.println("发布失败！");
                        }
                        ahp.main();
                        break;
                    case 2:
                        System.out.println("是否确定退出登录？");
                        System.out.println("1、确定");
                        System.out.println("2、取消");
                        int control = sc.nextInt();
                        if (control == 1) {
                            mp.main();
                        } else {
                            ahp.main();
                        }
                        break;
                    case 3:
                        usersList= usi.selectAllUser();
                        for (User user:usersList){
                                sb.append("id:  ").append(user.getUserId())
                                        .append("   is_admin:  ").append(user.getIsadmin())
                                        .append("   Username:  ").append(user.getUserName())
                                        .append("\n");
                        }
                        System.out.println("-------------------------");
                        System.out.println(sb.toString());
                        System.out.println("-------------------------");
                        ahp.main();
                        break;
                    case 4:
                        notesList = nni.selectAllNotes();
                        System.out.println("----------------------");
                        for (Note note : notesList) {
                            System.out.println(note);
                        }
                        System.out.println("----------------------");
                        System.out.println("请输入要删除的笔记的id：");
                        id = sc.nextLong();
                        int affectline = nni.deleteNoteById(id);
                        if (affectline > 0) {
                            System.out.println("删除成功!");
                        } else {
                            System.out.println("删除失败!");
                        }
                        ahp.main();
                        break;

                    case 5:
                        announceList = asi.selectAnnounce();
                        if(vlu.validate(announceList)) {
                            System.out.println("-----------------");
                            for (Announce announce : announceList) {
                                System.out.println(announce);
                            }
                            System.out.println("-----------------");
                            System.out.println("请输入要删除通告的id");
                            id = sc.nextLong();
                            int line = asi.deleteAnnounceById(id);
                            if (line > 0) {
                                System.out.println("删除成功！");
                            } else {
                                System.out.println("删除失败！");
                            }
                            return;
                        }
                        else {
                            System.out.println("暂无通告！");
                            return;
                        }
                      default:
                          System.out.println("输入错误！请输入正确的命令：");
                        break;
                }
            }catch (InputMismatchException e) {
                System.out.println("-----------------------");
                System.out.println("0、个人信息\t    1、发布通告  2、退出登录");
                System.out.println("3、查看用户信息\t4、删除用户笔记 \t5、删除通告");
                System.out.println("-----------------------");
                System.out.println("错误！请输入一个有效的数字选项！");
                sc.nextLine(); // 清除非法输入，等待下一次有效输入
            }

        }
    }




}